package com.feonix.common.util;

import java.io.IOException;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

import javax.servlet.ServletContext;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import freemarker.cache.MruCacheStorage;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;

public class FreemarkerUtil {

	private static final Log logger = LogFactory.getLog(FreemarkerUtil.class);

	private static Configuration configuration = null;

	public static void initFreemarker(ServletContext servletContext) {
		if (configuration != null) {
			return;
		}
		configuration = new Configuration();
		configuration.setDefaultEncoding("utf-8");
		configuration.setOutputEncoding("utf-8");
		configuration.setServletContextForTemplateLoading(servletContext, "/WEB-INF/templates");
		configuration.setEncoding(Locale.getDefault(), "utf-8");
		configuration.setCacheStorage(new MruCacheStorage(20, 10000));
		try {
			configuration.setNumberFormat("0.##");
			configuration.setSetting("template_update_delay", "3600");
			configuration.setSetting("whitespace_stripping", "true");
			configuration.setSetting("url_escaping_charset", "utf-8");
			configuration.setSetting("date_format", "yyyy-MM-dd");
			configuration.setSetting("time_format", "HH:mm:ss");
			configuration.setClassicCompatible(true);
			configuration.setSetting("datetime_format", "yyyy-MM-dd HH:mm:ss");
		} catch (TemplateException templateException) {
			logger.error(templateException);
		}
	}

	public static void setDebug(boolean paramBoolean) {
		try {
			if (configuration != null)
				if (paramBoolean)
					configuration.setSetting("template_update_delay", "0");
				else
					configuration.setSetting("template_update_delay", "3600");
		} catch (TemplateException localTemplateException) {
			logger.error(localTemplateException);
		}
	}

	/**
	 * 解析freemarker模板
	 * 
	 * @param ftlName
	 * @param params
	 * @return
	 */
	public static String processFTL(String ftlName, Map<String, ?> params) {
		StringWriter stringWriter = new StringWriter();
		Template template = null;
		String str = null;
		try {
			template = configuration.getTemplate(ftlName);
			template.process(params, stringWriter);
			str = stringWriter.toString();
		} catch (Exception exception) {
			logger.error(exception);
		} finally {
			try {
				stringWriter.close();
			} catch (IOException iOException2) {
				logger.warn(iOException2);
			}
		}
		return str;
	}

	/**
	 * 解析freemarker模板
	 * 
	 * @param ftlName
	 * @param key
	 * @param object
	 * @return
	 */
	public static String processFTL(String ftlName, String key, Object object) {
		HashMap<String, Object> hashMap = new HashMap<String, Object>();
		hashMap.put(key, object);
		return processFTL(ftlName, hashMap);
	}

}
